//package com.zkkz.boot.campus.config;
//
//import com.baomidou.mybatisplus.core.parser.ISqlParser;
//import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
//import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
//import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.annotation.MapperScan;
//import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.jdbc.DataSourceBuilder;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.transaction.annotation.EnableTransactionManagement;
//
//import javax.sql.DataSource;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * MyBatisPlus配置
// *
// * @author chan
// * @version 1.0
// * @Description MyBatisPlus配置
// * @date 2020/5/18 23:50
// */
//@Slf4j
////@EnableTransactionManagement
//@Configuration
////@MapperScan({/*"com.zkkz.boot.dds.mapper",*/"com.zkkz.boot.*.mapper"})
//public class MybatisPlusConfig {
//
//    //    @Bean("master")
////    @Primary
////    @ConfigurationProperties(prefix = "spring.datasource.hikari")
////    public DataSource master() {
////        return DataSourceBuilder.create().build();
////    }
////
////    @Bean("dynamicDataSource")
////    public DataSource dynamicDataSource() {
////        DynamicDataSource dynamicDataSource = new DynamicDataSource();
////        Map<Object, Object> dataSourceMap = new HashMap<>();
////        dataSourceMap.put("master", master());
////        // 将 master 数据源作为默认指定的数据源
////        dynamicDataSource.setDefaultDataSource(master());
////        // 将 master 和 slave 数据源作为指定的数据源
////        dynamicDataSource.setDataSources(dataSourceMap);
////        return dynamicDataSource;
////    }
//    @Bean
//    @ConfigurationProperties(prefix = "spring.datasource")
//    public DataSource dataSource() {
//        return DataSourceBuilder.create().build();
//    }
//
//    @Bean
//    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
//        /**
//         * 重点，使分页插件生效
//         */
//        Interceptor[] plugins = new Interceptor[1];
//        plugins[0] = paginationInterceptor();
//        sessionFactory.setPlugins(plugins);
//        //配置数据源，此处配置为关键配置，如果没有将 dynamicDataSource作为数据源则不能实现切换
//        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setTransactionFactory(new SpringManagedTransactionFactory());
//        //log.info("已配置数据源===MybatisPlusConfig=");
//        // 扫描Model
//        sessionFactory.setTypeAliasesPackage("com.zkkz.boot.*.pojo,com.zkkz.boot.*.pojo.*");
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        //log.info("扫描Model===MybatisPlusConfig=");
//        // 扫描映射文件
//        sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
//        log.info("扫描映射文件===MybatisPlusConfig=");
//        return sessionFactory.getObject();
//    }
//
//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        // 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
//        return new DataSourceTransactionManager(dataSource());
//    }
//
//    @Bean
//    public PaginationInterceptor paginationInterceptor() {
//        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//
//        List<ISqlParser> sqlParserList = new ArrayList<>();
//        // 攻击 SQL 阻断解析器、加入解析链
//        sqlParserList.add(new BlockAttackSqlParser());
//        paginationInterceptor.setSqlParserList(sqlParserList);
//        return paginationInterceptor;
//    }
//}
package com.zkkz.boot.campus.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
public class MybatisPlusConfig {
//    @Bean
////    @Primary
//    @ConfigurationProperties(prefix = "spring.datasource")
//    public DataSource dataSource() {
//        return DataSourceBuilder.create().build();
//    }
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        factoryBean.setPlugins(new PaginationInterceptor[]{new PaginationInterceptor()});
        return factoryBean.getObject();

    }
}
